package com.itheima.health.service.impl;

import com.itheima.health.dao.OrderSettingMapper;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@DubboService
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;

    /**
     * @Author: 位帅帅
     * @Date: 18:05 2021/1/9
     * @Params: [year, month]
     * @RerurnType: java.util.List<com.itheima.health.pojo.OrderSetting>
     * @Description: 查询预约数据
    */
    @Override
    public List<OrderSetting> findByMonth(Integer year, Integer month) {
        log.info("[预约-查询]year: {}, month: {}",year,month);
        String beginDate = String.format("%d-%d-1",year,month);
        String endDate = String.format("%d-%d-31",year,month);
        log.info("[预约-查询]beginDate: {}, endDate: {}",beginDate,endDate);
        Example example = new Example(OrderSetting.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andGreaterThanOrEqualTo("orderDate",beginDate);
        criteria.andLessThanOrEqualTo("orderDate",endDate);
        List<OrderSetting> orderSettingList = orderSettingMapper.selectByExample(example);
        log.info("[预约-查询]orderSettingList: {}",orderSettingList);
        return orderSettingList;
    }

    /**
     * @Author: 位帅帅
     * @Date: 19:49 2021/1/9
     * @Params: [orderSettingList]
     * @RerurnType: void
     * @Description: 批量上传设置预约人数
    */
    @Transactional
    @Override
    public void add(List<OrderSetting> orderSettingList) {
        log.info("[预约-上传]orderSettingList: {}",orderSettingList);
        for (OrderSetting orderSetting : orderSettingList) {
            orderSettingMapper.insertSelective(orderSetting);
        }
    }

    /**
     * @Author: 位帅帅
     * @Date: 12:54 2021/1/10
     * @Params: [orderSetting]
     * @RerurnType: void
     * @Description: 编辑预约数量
    */
    @Transactional
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        log.info("[预约-编辑预约数量]orderSetting: {}",orderSetting);
        //判断数据库是否存在预约信息
        Example example = new Example(OrderSetting.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderDate",orderSetting.getOrderDate());
        OrderSetting update = orderSettingMapper.selectOneByExample(example);
        if (null != update) {
            //存在则更新
            update.setNumber(orderSetting.getNumber());
            orderSettingMapper.updateByPrimaryKeySelective(update);
        } else {
            //不存在则插入
            orderSettingMapper.insertSelective(orderSetting);
        }
    }
}
